package mapa.escrevemapa;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class PlacesOverlay extends Overlay{
	
	private ArrayList<Ponto> pontos;
	private ArrayList<RectF> lugares;

	public PlacesOverlay(ArrayList<Ponto> p) {
		// TODO Auto-generated constructor stub
		this.pontos = p;
	}
	
	public void draw(Canvas canvas, MapView mapView, boolean shadow){
		super.draw(canvas, mapView, shadow);
		
		Bitmap bitmap = BitmapFactory.decodeResource(mapView.getResources(),R.drawable.hotel);
		Paint paint = new Paint();
		lugares = new ArrayList<RectF>();
		
		for(Ponto ponto: this.pontos){
			Point p = mapView.getProjection().toPixels(ponto.getGeoPoint(), null);
			RectF r = new RectF(p.x,p.y,p.x+bitmap.getWidth(),p.y+bitmap.getHeight());
			
			canvas.drawBitmap(bitmap, null, r, paint);
			this.lugares.add(r);
		}
	}
	
	public boolean onTap(GeoPoint geoPoint,MapView mapView)
	{	
		Point p = mapView.getProjection().toPixels(geoPoint, null);
		
		String name = new String();
		
		for(RectF r: this.lugares){
			if (r.contains(p.x, p.y)){
				for(Ponto ponto: this.pontos){
					Point p2 = mapView.getProjection().toPixels(ponto.getGeoPoint(), null);
					Log.v("PX", ""+p.x+" e "+ponto.getxE6());
					Log.v("PY", ""+p.y+" e "+ponto.getxE6());
					if(r.contains(p2.x,p2.y)){
						Log.v("PX", "DENTRO DO IF");
						name = ponto.getName();
					}
				}
				Intent intent = new Intent(mapView.getContext() , Comments.class);
				intent.putExtra("ENTRADA", name);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
				mapView.getContext().startActivity(intent);
				
				return true;
			}
		}		
		return false;
	}	
}
